package com.turing.post.tuxedo.util;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Element;

import com.turing.post.util.PCPSApplicationException;

/**
 * 支付交易输出
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
public class OPaid extends TransResponse implements Serializable {
	/**
	 * 本次交易产生的积分
	 */
	public BigInteger scoreAmount = BigInteger.ZERO;

	/**
	 * 本次交易实际扣除积分
	 */
	public BigInteger factScorePaidAmount = BigInteger.ZERO;

	/**
	 * 交易后积分余额
	 */
	public BigInteger score = BigInteger.ZERO;

	/**
	 * 卡数量
	 */
	public BigInteger cardNum = BigInteger.ZERO;

	/**
	 * 支付卡列表
	 */
	public List<CardPaidObj> cplist = null;

	/**
	 * 卡余额
	 */
	public Map<String, BigInteger> allCard = new HashMap<String, BigInteger>();

	/**
	 * 手续费/折扣
	 */
	public Map<String, BigInteger> allFee = new HashMap<String, BigInteger>();

	/**
	 * 交易费率(万分率)
	 */
	public BigInteger feeRatio = BigInteger.ZERO;

	/**
	 * 总扣费额(分)
	 */
	public BigInteger totalFee = BigInteger.ZERO;

	/**
	 * 产生计算MAC的域
	 */
	@Override
	protected void generateMacField() {
		appendMacField(cardNum.toString());
		for (Map.Entry<String, BigInteger> entry : allCard.entrySet()) {
			appendMacField(entry.getKey());
			appendMacField(entry.getValue().toString());
		}
	}

	/**
	 * 构造交易应答私有报文体
	 * 
	 * @param reqBody
	 * @throws Exception
	 */
	@Override
	protected void constructRespBodyXml(Element respBody)
			throws PCPSApplicationException {
		// 本次交易产生的积分
		TransXmlHelper.addStringElement(respBody,
				TransXmlHelper.XML_NODE_ScoreAmount, scoreAmount.toString());
		// 本次交易实际扣除积分
		TransXmlHelper.addStringElement(respBody,
				TransXmlHelper.XML_NODE_FactScorePaidAmount,
				factScorePaidAmount.toString());
		// 本次交易实际扣除积分
		TransXmlHelper.addStringElement(respBody,
				TransXmlHelper.XML_NODE_Score, score.toString());
		// 交易费率(万分率)
		TransXmlHelper.addStringElement(respBody,
				TransXmlHelper.XML_NODE_FeeRatio, feeRatio.toString());
		// 总扣费额(分)
		TransXmlHelper.addStringElement(respBody,
				TransXmlHelper.XML_NODE_TotalFee, totalFee.toString());
		// Paid
		Element paid_el = new Element(TransXmlHelper.XML_NODE_Paid);
		TransXmlHelper.addStringElement(paid_el,
				TransXmlHelper.XML_NODE_CardNum, cardNum.toString());
		if (cplist != null) {
			for (CardPaidObj cpo : cplist) {
				// each Card
				Element card_el = new Element(TransXmlHelper.XML_NODE_Card);
				String cardno = cpo.cardObj.cardNo;
				BigInteger bal = allCard.get(cardno);
				BigInteger fee = allFee.get(cardno);
				if (fee == null)
					fee = BigInteger.ZERO;
				TransXmlHelper.addStringElement(card_el,
						TransXmlHelper.XML_NODE_CardNo, cardno);
				TransXmlHelper.addStringElement(card_el,
						TransXmlHelper.XML_NODE_Fee, fee.toString());
				TransXmlHelper.addStringElement(card_el,
						TransXmlHelper.XML_NODE_Balance, bal.toString());
				paid_el.addContent(card_el);
			}
		}
		respBody.addContent(paid_el);
	}
}
